//
//  TTSpecificationViewCell.m
//  TaoTuo
//
//  Created by admin on 2019/8/26.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTSpecificationViewCell.h"
#import "TTDetailStockListModel.h"
#import "TTSubOrAddView.h"
#import "TTUpDownButton.h"
#import "TTUserModel.h"

@interface TTSpecificationViewCell ()

@property (nonatomic, strong) UIView *bgView;   // 背景view
@property (nonatomic, strong) UILabel *contentLabel;    // 内容
@property (nonatomic, strong) TTUpDownButton *locationBtn;    // 地点
@property (nonatomic, strong) UILabel *priceLabel;      // 价格
@property (nonatomic, strong) TTSubOrAddView *subOrAddView; // 添加或减少商品
@property (nonatomic, strong) UILabel *bottomLeftLabel; // 法诗欧……
@property (nonatomic, strong) UILabel *bottomRightLabel; // 现货或生产周期
@property (nonatomic, strong) UILabel *customBottomLeftLabel;   //做工，颜色
@property (nonatomic, strong) UILabel *customCountRightLabel;   //件数
@property (nonatomic, strong) UIImageView *rightImageView;        // 更多库存view
@property (nonatomic, strong) UILabel *rightLabel;      // 更多库存
@property (nonatomic, strong) UIImageView *arrowImageView;   // 右侧箭头

@property (nonatomic, strong) TTDetailStockListModel *model;
@property (nonatomic, assign) CellTypeSpecification cellType;   // 规格

@end

@implementation TTSpecificationViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];
        
        
    }
    return self;
}


- (void)setModel:(TTDetailStockListModel *)listModel specificationCellType:(CellTypeSpecification)specificationCellType categoryName:(nonnull NSString *)categoryName
{
    self.model = listModel;
    self.cellType = specificationCellType;
    // 行间距
    NSString *contentStr = [listModel.GoodsAttr componentsJoinedByString:@"\n"];
    NSMutableAttributedString *contentAttr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 5;
    [contentAttr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, contentStr.length)];
    self.contentLabel.attributedText = contentAttr;
    
    self.bottomLeftLabel.text = listModel.FactoryLabel;
    
    [self.locationBtn setTitle:listModel.LocationName forState:UIControlStateNormal];
    
    NSString *preStr = listModel.GoodsValuationPriceTypeText.length > 0 ? listModel.GoodsValuationPriceTypeText : @"";
    NSString *cha = listModel.PriceDiffRangText ? : @"";
    NSString *price = [NSString stringWithFormat:@"%@￥%@%@", preStr, listModel.Price, cha];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:price];
    [attr addAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : [UIColor redColor]} range:[price rangeOfString:listModel.Price]];
    [attr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:[price rangeOfString:@"￥"]];
    self.priceLabel.attributedText = attr;
    
    self.customBottomLeftLabel.text = [NSString stringWithFormat:@"做工:%@ 颜色:%@", listModel.ProductGradeName, listModel.GoldColor];
    self.customCountRightLabel.text = [NSString stringWithFormat:@"%d件", listModel.buyCount];
    
    BOOL isCustom = listModel.StockType == 2;
    
    self.bottomRightLabel.text = isCustom ? listModel.Cycle : listModel.StockTypeName;
    
    if (specificationCellType == CellTypeWithSpecificationResult) {
        self.bgView.layer.borderColor = kMainColor.CGColor;
        self.bgView.backgroundColor = [UIColor whiteColor];
        
        self.subOrAddView.hidden = YES;
        self.rightImageView.hidden = NO;
        
        self.customBottomLeftLabel.hidden = !isCustom;
        self.customCountRightLabel.hidden = !isCustom;
        
    } else {
        
        UIColor *borderColor = listModel.selected ? kMainColor : UIColorHex(ffffff);
        UIColor *bgColor = listModel.selected ? UIColorHex(ffffff) : kSeparatorColor;
        self.bgView.layer.borderColor = borderColor.CGColor;
        self.bgView.backgroundColor = bgColor;
        
        self.subOrAddView.hidden = !isCustom;
        self.customBottomLeftLabel.hidden = !isCustom;
        self.customCountRightLabel.hidden = YES;
        self.rightImageView.hidden = YES;
    }
    
    
    [self setupSubviews];
    
    self.subOrAddView.currentValue = listModel.buyCount;
    self.subOrAddView.minValue = listModel.BuyNumberMin;
    self.subOrAddView.maxValue = listModel.BuyNumberMax;
    
    UIColor *color = listModel.buyCount <= listModel.BuyNumberMin ? UIColorHex(EFEFEF) : [UIColor whiteColor];
    self.subOrAddView.subLabel.layer.backgroundColor = color.CGColor;
}



- (void)setupSubviews
{
    CGFloat rightViewW = self.rightImageView.hidden ? 0 : 30;
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(5);
        make.right.bottom.equalTo(self.contentView).mas_offset(-5);
    }];
    
//    CGFloat contentW = SCREEN_WIDTH >= 375 ? 290 * kScale : 270 * kScale;
    CGFloat contentW = SCREEN_WIDTH - 20 - rightViewW;
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(5);
        make.left.equalTo(self.bgView).mas_offset(5);
        make.width.mas_equalTo(contentW);
    }];
    
    [self.rightImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(self.bgView);
        make.width.mas_equalTo(rightViewW);
    }];

    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(self.rightImageView);
        make.width.mas_equalTo(rightViewW * 2 / 3);
    }];

    [self.arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rightLabel.mas_right);
        make.top.bottom.right.equalTo(self.rightImageView);
    }];

    [self.locationBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel);
        make.right.equalTo(self.rightImageView.mas_left);
        make.height.mas_equalTo(20 * kScale);
        make.width.mas_equalTo(30 * kScale);
    }];

    [self.bottomRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).mas_offset(1);
        make.bottom.equalTo(self.bgView).mas_offset(-5);
        make.right.equalTo(self.rightImageView.mas_left).mas_offset(-3);
    }];
    
    [self.bottomLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottomRightLabel);
        make.left.equalTo(self.contentLabel);
    }];


    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bottomRightLabel.mas_top).mas_offset(-3);
        make.right.equalTo(self.bottomRightLabel);
    }];


    [self.customBottomLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bgView.mas_bottom).mas_offset(-5);
        make.left.equalTo(self.contentLabel);
    }];
    
    [self.customCountRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.priceLabel);
        make.bottom.equalTo(self.customBottomLeftLabel);
    }];
    
    [self.subOrAddView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.equalTo(self.customCountRightLabel);
        make.width.mas_equalTo(106);
        make.height.mas_equalTo(22);
    }];
    
    BOOL isCustom = self.model.StockType == 2;
    
    if (self.cellType == CellTypeWithSpecificationChoose) {
        if (isCustom) {
            
            [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.contentLabel);
                make.right.equalTo(self.bgView).mas_offset(-5);
            }];
            
            [self.bottomRightLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.priceLabel.mas_bottom).mas_offset(1);
                make.right.equalTo(self.priceLabel);
            }];
        }
    } else {
        if (isCustom) {
            
            [self.customCountRightLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(self.contentLabel);
                make.bottom.equalTo(self.customBottomLeftLabel);
            }];
            
            [self.bottomRightLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.customCountRightLabel.mas_top).mas_offset(-5);
                make.right.equalTo(self.customCountRightLabel);
            }];
            
            [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.bottomRightLabel.mas_top).mas_offset(-5);
                make.right.equalTo(self.customCountRightLabel);
            }];
        }
    }
    
    
}

- (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] init];
        _bgView.layer.cornerRadius = 5;
        _bgView.layer.borderWidth = 1.0;
        [self.contentView addSubview:_bgView];
    }
    return _bgView;
}

- (UILabel *)contentLabel
{
    if (!_contentLabel) {
        _contentLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
        _contentLabel.numberOfLines = 0;
        [self.bgView addSubview:_contentLabel];
    }
    return _contentLabel;
}

- (UIButton *)locationBtn
{
    if (!_locationBtn) {
        _locationBtn = [TTUpDownButton buttonWithType:UIButtonTypeCustom];
        [_locationBtn setImage:[UIImage imageNamed:@"didian"] forState:UIControlStateNormal];
        [_locationBtn setTitleColor:UIColorHex(DEDEDE) forState:UIControlStateNormal];
        _locationBtn.titleLabel.font = [UIFont systemFontOfSize:9];
        [self.bgView addSubview:_locationBtn];
    }
    return _locationBtn;
}


- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(DEDEDE) fontSize:9];
        [self.bgView addSubview:_priceLabel];
    }
    return _priceLabel;
}

- (TTSubOrAddView *)subOrAddView
{
    if (!_subOrAddView) {
        @weakify(self)
        _subOrAddView = [TTSubOrAddView subAddViewWithMin:self.model.BuyNumberMin max:self.model.BuyNumberMax clickBtnBlock:^(int currentValue) {
            @strongify(self)
            self.model.buyCount = currentValue;
        }];
        [self.bgView addSubview:_subOrAddView];
    }
    return _subOrAddView;
}

- (UILabel *)bottomLeftLabel
{
    if (!_bottomLeftLabel) {
        _bottomLeftLabel = [UILabel labelWithTitle:@"" color:UIColorHex(8573BA) fontSize:12];
        [self.bgView addSubview:_bottomLeftLabel];
    }
    return _bottomLeftLabel;
}

- (UILabel *)bottomRightLabel
{
    if (!_bottomRightLabel) {
        _bottomRightLabel = [UILabel labelWithTitle:@"" color:UIColorHex(8573BA) fontSize:12];
        [self.bgView addSubview:_bottomRightLabel];
    }
    return _bottomRightLabel;
}

- (UILabel *)customBottomLeftLabel
{
    if (!_customBottomLeftLabel) {
        _customBottomLeftLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
        [self.bgView addSubview:_customBottomLeftLabel];
    }
    return _customBottomLeftLabel;
}

- (UILabel *)customCountRightLabel
{
    if (!_customCountRightLabel) {
        _customCountRightLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
        [self.bgView addSubview:_customCountRightLabel];
    }
    return _customCountRightLabel;
}

- (UIView *)rightImageView
{
    if (!_rightImageView) {
        _rightImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"detail_more_right"]];
//        _rightImageView.backgroundColor = UIColorHex(8858F8);
        _rightImageView.contentMode = UIViewContentModeScaleToFill;
        [self.bgView addSubview:_rightImageView];
        
        _rightLabel = [UILabel labelTextCenterWithTitle:@"更多库存" color:[UIColor whiteColor] fontSize:13];
        _rightLabel.numberOfLines = 0;
        [_rightImageView addSubview:_rightLabel];
        
        
        _arrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"white_right_arrow"]];
        _arrowImageView.contentMode = UIViewContentModeCenter;
        [_rightImageView addSubview:_arrowImageView];
        
        
    }
    return _rightImageView;
}


@end
